package pl.pollub.medikit.client.ui;

import pl.pollub.medikit.client.AuthService;
import pl.pollub.medikit.client.AuthServiceAsync;
import pl.pollub.medikit.client.Medikit;
import pl.pollub.medikit.client.callbacks.AuthCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

public class UILoginPanel extends DeckPanel {
	public UILoginPanel(final Medikit appRoot) {
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		add(absolutePanel);
		absolutePanel.setHeight("349px");
		
		Label lblLogin = new Label("Login");
		absolutePanel.add(lblLogin, 30, 54);
		
		final TextBox loginTb = new TextBox();
		absolutePanel.add(loginTb, 27, 76);
		loginTb.setSize("388px", "16px");
		
		Label lblHaso = new Label("Hasło");
		absolutePanel.add(lblHaso, 30, 122);
		lblHaso.setSize("31px", "16px");
		
		final PasswordTextBox passwordTextBox = new PasswordTextBox();
		absolutePanel.add(passwordTextBox, 30, 144);
		passwordTextBox.setSize("385px", "16px");
		
		Label lblRola = new Label("Rola");
		absolutePanel.add(lblRola, 30, 194);
		
		final ListBox comboBox = new ListBox();
		comboBox.addItem("Lekarz");
		comboBox.addItem("Pacjent");
		absolutePanel.add(comboBox, 30, 216);
		comboBox.setSize("395px", "20px");
		
		Button btnLogin = new Button("New button");
		btnLogin.setText("Zaloguj");
		absolutePanel.add(btnLogin, 171, 268);
		btnLogin.setSize("107px", "28px");
		
		btnLogin.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				AuthCallback loginCallback = new AuthCallback(appRoot);
				AuthServiceAsync authService = GWT.create(AuthService.class);

				String login = loginTb.getValue();
				String pass = passwordTextBox.getValue();
				int role = comboBox.getSelectedIndex();
			
				if ((null != login) && (null != pass)) {
					authService.validUser(login, pass, role, loginCallback);
				}
			}
		});
		
		Hyperlink hprlnkNewHyperlink = new Hyperlink("Nowe konto", false, "newHistoryToken");
		
		absolutePanel.add(hprlnkNewHyperlink, 191, 308);
		
		Label lblLogowanie = new Label("Logowanie");
		absolutePanel.add(lblLogowanie, 194, 10);
		showWidget(0);
		
	}
}
